package com.hdey.controller.chao;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hdey.mapper.chao.ChukuMapper;
import com.hdey.mapper.chao.ShenlingMapper;
import com.hdey.mapper.chao.ShenlingziMapper;
import com.hdey.pojo.chao.Chuku;
import com.hdey.pojo.chao.Shenling;
import com.hdey.service.chao.ChukuService;
import com.hdey.service.chao.ChukuziService;
import com.hdey.service.chao.ShenlingService;
import com.hdey.service.chao.ShenlingziService;
import com.hdey.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;

@RestController
@RequestMapping("/ck")
@Slf4j
public class ChukuController {

        @Resource
        private ShenlingService shenlingService;

        @Resource
        private ShenlingMapper shenlingMapper;

        @Resource
        private ShenlingziMapper shenlingziMapper;

        @Resource
        private ShenlingziService shenlingziService;

        @Resource
        private ChukuService chukuService;

        @Resource
        private ChukuMapper chukuMapper;

        @Resource
        private ChukuziService chukuziService;


        private String msg;

        @GetMapping("/index")
        public ModelAndView index(){

            return new ModelAndView("chao/chuku");

        }
//        // 添加
//        @RequestMapping("/add")
//        public Result add(Wuzi wuzi) {
//                System.out.println(wuzi);
//                wuzi.setCreateDate(LocalDate.now());
//                boolean flag = wuziService.save(wuzi);
//                return flag ? Result.success("添加成功") : Result.fail("添加失败");
//        }

        @RequestMapping("/search")
        @ResponseBody
        public Result search(Integer page, Integer limit, String outstorageMasterNo){
                Page<Chuku> chukuPage = chukuService.searchPage(page, limit, outstorageMasterNo);
                System.out.println(chukuPage.getRecords());
                return Result.success(chukuPage);
        }





        @RequestMapping("/update")
        public Result update(Integer outstorageMasterId){

                Chuku chuku1 = new Chuku();
                chuku1.setOutstorageMasterId(outstorageMasterId);
                chuku1.setOutstorageMasterStatus("1");

                Integer applyMasterId=outstorageMasterId;
                Shenling shenling=new Shenling();
                shenling.setApplyMasterId(applyMasterId);
                shenling.setApplyMasterStatuses(1);



                shenlingMapper.updateById(shenling);

                //chukuService.updateById(chuku1);
                int flag=chukuMapper.updateById(chuku1);
                String msg=null;
                if (flag==1){
                        msg="申领物资已出库";
                }else {
                        msg="申领物资出库失败";
                }
                return Result.failed(msg);
        }


        /*@PostMapping("/update")
        public Result update(Integer outstorageMasterId){
                log.error(outstorageMasterId.toString());
                System.out.println(outstorageMasterId);
                log.error("dfs");
                try {
                        chukuService.chukuUpdate(outstorageMasterId);
                        msg="申领物资已出库";
                        return Result.success(msg);
                }catch (Exception ex){
                        msg="申领物资出库失败";
                        log.error(msg,ex);
                        return Result.failed(msg);
                }

        }*/


}
